package com.spring.gop.proj.controller;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.spring.gop.proj.interfaces.NoticeService;
import com.spring.gop.proj.model.Notice;

@Controller
@RequestMapping("/help/notice/*")
@SessionAttributes("notice")
public class NoticeController {

	@Autowired private NoticeService noticeService;
	
	@RequestMapping
	public void list(Model model) {
		model.addAttribute("pageIdx", 4);
		model.addAttribute("notices", noticeService.selectNotices());
	}
	
	@RequestMapping(value="/read", method=RequestMethod.GET)
	public Notice read(Model model, HttpSession session, int num) {
		model.addAttribute("pageIdx", 5);
		session.setAttribute("listNum", num);
		return noticeService.selectNotice(num);
	}
	
	@RequestMapping(value="/write", method=RequestMethod.GET)
	public void writeForm() {}
	
	@RequestMapping(value="/write", method=RequestMethod.POST)
	public String write(@ModelAttribute("newNotice")Notice notice) {
		noticeService.insertNotice(notice);
		return "redirect:list";
	}
}
